<?php if (!defined("_ECRIRE_INC_VERSION")) return; if (class_exists('PdkRubriqueModel')) return;
/**
 * SPIP Plugin Development Kit
 *
 * @author     Aleksey Yaroshchuk (aleksey.yaroshchuk@gmail.com)
 * @license    GNU/GPL
 * @package    plugins
 * @subpackage pdk
 * @category   outils
 * @copyright  (c) Aleksey Yaroshchuk 2011-2012
 * @link       http://www.spip-contrib.net/Developmet-Kit
 * @version    1.0.0
 */

include_spip('classes/pdk_model');
/** 
 *  This is PDK SPIP Rubrique model
 *
 *  @author Aleksey Yaroshchuk (aleksey.yaroshchuk@gmail.com)
 */
class PdkRubriqueModel extends pdk_Model {
    public function __construct() {
        parent::__construct();
        $this->config_model();        
    }
    
    function config_model() {
        // Define main table:
        $this->main_table = 'spip_rubriques';
        
        // Define primary key:
        $this->primary_key = 'id_rubrique';
        
        // Define main table fields:
        $this->main_fields['id_rubrique'] = 'id_rubrique'; 
        $this->main_fields['id_parent'] = 'id_parent';
        $this->main_fields['titre'] = 'titre'; 
        $this->main_fields['id_secteur'] = 'id_secteur';
        $this->main_fields['lang'] = 'lang';
        /*$this->main_fields['descriptif'] = 'descriptif';
        $this->main_fields['id_secteur'] = 'id_secteur';
        $this->main_fields['maj'] = 'maj';
        $this->main_fields['export'] = 'export';
        $this->main_fields['id_import'] = 'id_import';
        $this->main_fields['statut'] = 'statut';
        $this->main_fields['date'] = 'date';
        $this->main_fields['lang'] = 'lang';
        $this->main_fields['langue_choisie'] = 'langue_choisie';
        $this->main_fields['extra'] = 'extra';
        $this->main_fields['statut_tmp'] = 'statut_tmp';
        $this->main_fields['date_tmp'] = 'date_tmp';
           */                                                                      
         // Define validation rules:
         $this->validation->set_rules( 
            array(
               array(
                     'field'   => 'id_rubrique',
                     'label'   => 'ID rubrique',
                     'rules'   => ''
                  )
            )
         );                                     
    }
    
    // Define test data for model
    public $test_data = array(
        array('id_rubrique'=>1,'titre'=>'Test rubrique 1','id_parent'=>'0','lang'=>'ru','id_secteur'=>1),
        array('id_rubrique'=>2,'titre'=>'Test rubrique 12','id_parent'=>'1','lang'=>'ru','id_secteur'=>1),
        array('id_rubrique'=>3,'titre'=>'Test rubrique 123','id_parent'=>'2','lang'=>'en','id_secteur'=>1),
        array('id_rubrique'=>4,'titre'=>'Test rubrique 4','id_parent'=>'0','lang'=>'en','id_secteur'=>4)
    );
    
    /**
     *  Check whether one rubrique belongs to another rubrique
     *  @param int $id_rubrique ID rubriques to check
     *  @param int $id_rubrique_belong ID rubrique to wich $id_rubrique may possibly belong
     */
    public function CheckRubriqueInRubrique($id_rubrique, $id_rubrique_belong) {
		if ($id_rubrique == $id_rubrique_belong) return true;
        $rubrique = $this->select_by_id($id_rubrique);
        if ($rubrique['id_parent'] == $id_rubrique_belong)
            return true;
        if ($rubrique['id_parent'] != 0)
            return $this->CheckRubriqueInRubrique($rubrique['id_parent'], $id_rubrique_belong);
        return false;
    }
}
?>